﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Security.Cryptography;
using System.IO;

public static class EncryptionHelper
{
    private static byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
    {
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = Key;
        alg.IV = IV;
        CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write);
        cs.Write(clearData, 0, clearData.Length);
        cs.Close();
        byte[] encryptedData = ms.ToArray();
        return encryptedData;
    }

    /// <summary>
    /// Verilen texti sifreler.
    /// Not : Şifreleme için ihtiyaç duyduğu anahtar değerini application.config dosyasından almaktadır.
    /// </summary>
    /// <param name="clearText">Şifrelenicek Text</param>
    /// <returns>string</returns>
    public static string Encrypt(string clearText)
    {
        string encriptedText = string.Empty;
        string retValue = string.Empty;

        string Password = ConfigurationManager.AppSettings["anahtar"].ToString();
        byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        encriptedText = Convert.ToBase64String(encryptedData);
        retValue = encriptedText.Replace("+", "*");
        return retValue;
    }

    private static byte[] Encrypt(byte[] clearData)
    {
        string Password = ConfigurationManager.AppSettings["anahtar"].ToString();
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        return Encrypt(clearData, pdb.GetBytes(32), pdb.GetBytes(16));
    }

    private static byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
    {
        MemoryStream ms = new MemoryStream();
        Rijndael alg = Rijndael.Create();
        alg.Key = Key;
        alg.IV = IV;
        CryptoStream cs = new CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write);
        cs.Write(cipherData, 0, cipherData.Length);
        cs.Close();
        byte[] decryptedData = ms.ToArray();
        return decryptedData;
    }

    /// <summary>
    /// Verilen şifreli texti deşifre eder.
    /// Not : Deşifreleme için ihtiyaç duyduğu anahtar değerini application.config dosyasından almaktadır.
    /// </summary>
    /// <param name="cipherText">Şifrelenmiş Text</param>
    /// <returns>string</returns>
    public static string Decrypt(string cipherText)
    {
        string sifreliText = string.Empty;
        string retValue = string.Empty;

        string Password = ConfigurationManager.AppSettings["anahtar"].ToString();
        sifreliText = cipherText.Replace("*", "+");
        byte[] cipherBytes = Convert.FromBase64String(sifreliText);
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
        retValue = System.Text.Encoding.Unicode.GetString(decryptedData);
        return retValue;

    }

    private static byte[] Decrypt(byte[] cipherData)
    {
        string Password = ConfigurationManager.AppSettings["anahtar"].ToString();
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
        return Decrypt(cipherData, pdb.GetBytes(32), pdb.GetBytes(16));
    }
}
